<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace addons\video\user\controller;


/**
 * Description of VideoManage
 * 视频管理
 * @author zmh
 */
class VideoManage extends \web\user\controller\AddonUserBase{

    public function index(){
        $classM = new \addons\video\model\VideoClass();
        $list = $classM->getDataList(-1,-1,'1=1','id,class_title', 'order_index desc');
        $this->assign('class_list', $list);
        return $this->fetch();
    }

    /**
     * 审核视频
     * @return array
     */
    public function auth_status(){
        $id = $this->_post('id');
        $status = $this->_post('status');
        $m = new \addons\video\model\Video();
        try{
            $ret = $m->where(['id' => $id])->update(['status' => $status]);
            if(!$ret)return $this->failData('审核失败，请勿重复提交');
            return $this->successData();
        } catch (\Exception $ex) {
            return $this->failData($ex->getMessage());
        }
    }

    /**
     * 直播列表
     * @return array
     */
    public function loadList(){
        $keyword = $this->_get('keyword');
        $status = $this->_get('status');
        $phone = $this->_get('phone');
        $class_id = $this->_get('class_id');
        $filter = ' 1=1 ';
        if ($keyword != null) {
            $filter .= ' and b.username like \'%' . $keyword . '%\'';
        }
        if ($class_id != null) {
            $filter .= ' and a.class_id = ' . $class_id;
        }
        if ($status != null) {
            $filter .= ' and a.status = ' . $status;
        }
        if ($phone != null) {
            $filter .= ' and b.phone like \'%' . $phone . '%\'';
        }
        $order = 'a.develop_time desc,a.id desc';

        $m = new \addons\video\model\Video();
        $total = $m->getVideoTotal($filter);
        $rows = $m->getVideoList($this->getPageIndex(),$this->getPageSize(), $filter,'a.*,b.username,b.phone,c.class_title',$order);
        return $this->toDataGrid($total, $rows);
    }

    public function loadData(){
        $id = $this->_get('id');
        $m = new \addons\video\model\Video();
        $data = $m->getDetail($id);
        if($data['video_type']==0){
            $data['class_id_s'] = $data['class_id'];
        }else{
            $data['class_id_f'] = $data['class_id'];
        }
        $data['tags'] = explode(',', $data['tags']);
        $data['video_img_t'] = $data['video_img'];
        // $data['video_length_t'] = $data['video_length'];
        $data['video_path_t'] = $data['video_path'];
        return $data;
    }

    public function play(){
        $this->assign('path', $this->_get('path'));
        return $this->fetch();
    }
    
    /**
     * 禁止或通过
     */
    public function changeStatus(){
        $id = $this->_get('id');
        $m = new \addons\video\model\Video();
        $info = $m->getDetail($id,'id,status,phone');
        $info['status'] = $info['status']?0:1;
        $res = $m->save($info);
        if($res){
            return $this->successData();
        }else{
            return $this->failData('操作账号失败');
        }
    }
    
    public function edit(){
        $m = new \addons\video\model\Video();
        if(IS_POST){
            $data = $_POST;
            if($data['video_type']==0){
                $data['class_id'] = $data['class_id_s'];
            }else{
                $data['class_id'] = $data['class_id_f'];
            }
            if(!empty($data['tags'])){
                $data['tags'] = implode(',', $data['tags']);
            }
            if($data['set_type']==1){
                $data['video_img'] = $data['video_img_t'];
                $data['video_length'] = $data['video_length_t'];
                $data['video_path'] = $data['video_path_t'];
            }
            $data['update_time'] = NOW_DATETIME;
            if($data['id']){
                $m->save($data);
                return $this->successData();
            }else{
                $data['create_time'] = NOW_DATETIME;
                $data['video_view'] = 0;
                $m->add($data);
                return $this->successData();
            }
        }else{
            $list = (new \addons\member\model\MemberAccountModel)->field('id,username')->where('logic_delete=0 and is_actor=1')->order('id asc')->select();
            $this->assign('user_list', json_encode($list, 256));
            $class_list = (new \addons\video\model\VideoClass)->field('id,class_title')->where('is_show=1 and video_type=0')->order('order_index asc')->select();
            $class_list_f = (new \addons\video\model\VideoClass)->field('id,class_title')->where('is_show=1 and video_type=1')->order('order_index asc')->select();
            $tags_list = (new \addons\video\model\VideoTags)->field('id,tags_name')->where('is_on=1')->order('order_index asc')->select();
            $this->assign('class_list', $class_list);
            $this->assign('class_list_f', $class_list_f);
            $this->assign('tags_list', $tags_list);
            $this->assign('id', $this->_get('id'));
            $this->setLoadDataAction('loadData');
            return $this->fetch();
        }
    }


    public function add(){
        $m = new \addons\video\model\Video();
        if(IS_POST){
            $data = $_POST;
            if($data['start']>$data['end']){
                return $this->failData('开始数量大于结束数量');
            }
            if($data['start']<=0||$data['end']<=0){
                return $this->failData('开始数量与结束数量,不能为空');
            }
            if($data['id']){
                $number = rand($data['start'],$data['end']);
                $m->where('id',$data['id'])->setInc('praise_number',$number);
                return $this->successData();
            }else{
                $m->query('update tp_video set  praise_number = praise_number + CAST(RAND()*'.intval($data['end']).'+'.intval($data['start']).' AS SIGNED)');
                return $this->successData();
            }
        }else{
            $this->assign('id', $this->_get('id'));
            return $this->fetch();
        }
    }


    public function del(){
        $id = intval($this->_post('id'));
        if (!empty($id)) {
            $m = new \addons\video\model\Video();
            try {
                $res = $m->deleteData($id);
                if ($res > 0) {
                    return $this->successData();
                }
            } catch (\Exception $e) {
                return $this->failData($e->getMessage());
            }
        } else {
            return $this->failData('删除失败，参数有误');
        }
    }

    /**
     * 审核列表
     */
    public function examine_index(){
        $classM = new \addons\video\model\VideoClass();
        $list = $classM->getDataList(-1,-1,'1=1','id,class_title', 'order_index desc');
        $this->assign('class_list', $list);

        return $this->fetch();
    }

    /**
     * 直播列表
     * @return array
     */
    public function examineLoadList(){
        $keyword = $this->_get('keyword');
        $status = $this->_get('status');
        $phone = $this->_get('phone');
        $class_id = $this->_get('class_id');
        $filter = ' status = 0 ';
        if ($keyword != null) {
            $filter .= ' and b.username like \'%' . $keyword . '%\'';
        }
        if ($class_id != null) {
            $filter .= ' and a.class_id = ' . $class_id;
        }
        if ($status != null) {
            $filter .= ' and a.status = ' . $status;
        }
        if ($phone != null) {
            $filter .= ' and b.phone like \'%' . $phone . '%\'';
        }
        $order = 'a.develop_time desc,a.id desc';
        $m = new \addons\video\model\Video();
        $total = $m->getVideoTotal($filter);
        $rows = $m->getVideoList($this->getPageIndex(),$this->getPageSize(), $filter,'a.*,b.username,b.phone,c.class_title',$order);
        return $this->toDataGrid($total, $rows);
    }


    /**
     * 置顶
     * @return array
     */
    public function is_develop(){
        try {
            $id = intval($this->_post('id'));
            $type = $this->_post('type');
            $m = new \addons\video\model\Video();
            if($type == 1){
                $res = $m->where(['id' => $id])->update(['develop_time' => NOW_DATETIME]);
            }else{
                $res = $m->where(['id' => $id])->update(['develop_time' => '0000-00-00 00:00:00']);
            }
            if ($res > 0) {
                return $this->successData();
            }
        } catch (\Exception $e) {
            return $this->failData($e->getMessage());
        }
    }

}
